package com.ruoyi.config;

import com.ruoyi.common.config.QiniuFileStoreConfig;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.thymeleaf.spring5.view.ThymeleafViewResolver;

import java.util.HashMap;
import java.util.Map;

/**
 * thymeleaf上下文中增加全局变量，方便引用
 */
@Configuration
public class ViewResolverConfig {
    @Autowired
    @Qualifier("thymeleafViewResolver")
    private ThymeleafViewResolver thymeleafViewResolver;

    @Bean
    public void myViewConfig() {
        if (thymeleafViewResolver != null) {
            String imgDomain = StringUtils.EMPTY;
            Map<String, Object> map = new HashMap<>();
            //当开启七牛云存储时，将域名放入到thymeleaf的全局变量中，方便页面直接取图片域名
            if(QiniuFileStoreConfig.getEnabled())
            {
                imgDomain = QiniuFileStoreConfig.getDomain();
            }
            map.put("imgDomain", imgDomain);
            thymeleafViewResolver.setStaticVariables(map);

        }
    }
}
